package com.jic.member.controller.power;


import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.member.common.util.BeanUtil;
import com.jic.member.entity.power.MememberLegalRule;
import com.jic.member.request.power.MememberLegalRuleRequest;
import com.jic.member.response.power.MememberLegalRuleResponse;
import com.jic.member.service.power.MememberLegalRuleService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: zxl
 * @date: 2020-01-09
 * @remark: 会员等级权益规则controller
 */

@ApiOperation(value = "会员等级权益规则controller")
@RestController
@RequestMapping("/memberLegalRule")
@Slf4j
public class MememberLegalController {

    @Autowired
    MememberLegalRuleService mememberLegalRuleService;

    /*
     * 会员等级权益分页查询
     * */
    @ApiOperation(value = "获取会员等级权益所有列表")
    @ResponseBody
    @RequestMapping("/queryMemberLegalRuleList")
    public RestResult<PageResult<MememberLegalRuleResponse>> queryMemberLegalRuleList(@RequestBody MememberLegalRuleRequest form) {
        log.info("开始进入会员等级权益列表=========》,{}", form.toString());
        Page page = new Page();
        page.setLimit(form.getLimit());
        page.setStart(form.getStart());
        MememberLegalRule memberRequest = BeanUtil.copy(form, MememberLegalRule.class);
        PageResult<MememberLegalRule> result = mememberLegalRuleService.queryPage(memberRequest, page);
        List<MememberLegalRuleResponse> memberResponseList = result.getRows().stream().map(temp -> {
            MememberLegalRuleResponse memberResponse = BeanUtil.copy(temp, MememberLegalRuleResponse.class);
            return memberResponse;
        }).filter(item -> item != null).collect(Collectors.toList());
        PageResult<MememberLegalRuleResponse> pageResult = new PageResult<>();
        pageResult.setRows(memberResponseList);
        pageResult.setTotal(result.getTotal());
        return RestResult.success(pageResult);
    }

    /*
     * 会员等级权益分页查询
     * */
    @ApiOperation(value = "获取会员等级权益所有列表")
    @ResponseBody
    @RequestMapping("/queryMemberLegalRuleListOther")
    public RestResult queryMemberLegalRuleListOther(@RequestBody MememberLegalRuleRequest form) {
        log.info("开始进入会员等级权益列表=========》,{}", form.toString());
        MememberLegalRule memberRequest = BeanUtil.copy(form, MememberLegalRule.class);
        List<MememberLegalRule> result = mememberLegalRuleService.query(memberRequest);
        return RestResult.success(result);
    }


    /**
     * 编辑会员等级权益
     * @param form
     * @return
     */
    @ApiOperation(value="编辑会员等级权益", notes="编辑会员等级权益")
    @ResponseBody
    @RequestMapping("/editMemberLegalInfo")
    public RestResult<String> editMemberLegalInfo(@RequestBody MememberLegalRuleRequest form) {
        log.info("开始进入编辑会员等级权益=========》,{}", form.toString());
        RestResult restResult = new RestResult<>();
        try {
            restResult =mememberLegalRuleService.insertMemberLegal(form);
        } catch (Exception e) {
            log.error("error",e);
            restResult=RestResult.error("9999",e.getLocalizedMessage());
        }
        return restResult;
    }


    /**
     * 删除等级权益
     * @param form
     * @return
     */
    @ApiOperation(value="删除等级权益", notes="删除等级权益")
    @ResponseBody
    @RequestMapping("/deletMemberLegalInfo")
    public RestResult<String> deletMemberInfo(@RequestBody MememberLegalRuleRequest form) {
        RestResult restResult = new RestResult<>();
        try {
            if (form.getId() !=null){
                restResult=mememberLegalRuleService.deletMemberInfo(form);
            }
        } catch (Exception e) {
            log.error("error",e);
            restResult=RestResult.error("9999",e.getLocalizedMessage());
        }
        return restResult;
    }

    /**
     * 获取会员积分规则
     * @param form
     * @return
     */
    @ApiOperation(value="获取会员权益规则", notes="获取会员权益规则")
    @ResponseBody
    @RequestMapping("/queryMemberPowerInfo")
    public RestResult queryMemberPowerInfo(@RequestBody MememberLegalRuleRequest form) {
        MememberLegalRuleResponse memberLegalRule = mememberLegalRuleService.queryMemberPowerInfo(form);
        return RestResult.success(memberLegalRule);
    }

    /**
     * 编辑会员权益信息
     * @param form
     * @return
     */
    @ApiOperation(value="编辑会员权益信息", notes="编辑会员权益信息")
    @ResponseBody
    @RequestMapping("/editMemberPowerInfo")
    public RestResult<String> editMemberPowerInfo(@RequestBody MememberLegalRuleRequest form) {
        log.info("开始进入编辑会员等级权益=========》,{}", form.toString());
        RestResult restResult = new RestResult<>();
        try {
            restResult =mememberLegalRuleService.editMemberPowerInfo(form);
        } catch (Exception e) {
            log.error("error",e);
            restResult=RestResult.error("9999",e.getLocalizedMessage());
        }
        return restResult;
    }

    /*
     * 获取店铺会员等级权益所有列表
     * */
    @ApiOperation(value = "获取店铺会员等级权益所有列表")
    @ResponseBody
    @RequestMapping("/queryStoreMemberLegalRuleList")
    public RestResult<PageResult<MememberLegalRuleResponse>> queryStoreMemberLegalRuleList(@RequestBody MememberLegalRuleRequest form) {
        log.info("开始进入会员等级权益列表=========》,{}", form.toString());
        Page page = new Page();
        page.setLimit(form.getLimit());
        page.setStart(form.getStart());
        MememberLegalRule memberRequest = BeanUtil.copy(form, MememberLegalRule.class);
        PageResult<MememberLegalRule> result = mememberLegalRuleService.queryStoreMemberLegalRuleList(memberRequest, page);
        List<MememberLegalRuleResponse> memberResponseList = result.getRows().stream().map(temp -> {
            MememberLegalRuleResponse memberResponse = BeanUtil.copy(temp, MememberLegalRuleResponse.class);
            return memberResponse;
        }).filter(item -> item != null).collect(Collectors.toList());
        PageResult<MememberLegalRuleResponse> pageResult = new PageResult<>();
        pageResult.setRows(memberResponseList);
        pageResult.setTotal(result.getTotal());
        return RestResult.success(pageResult);
    }

}
